package com.lebang.controller.api;

import com.lebang.aop.Sessionid;
import com.lebang.common.Require;
import com.lebang.common.validcode.RedisLogin;
import com.lebang.common.validcode.ValidCodeStatus;
import com.lebang.common.validcode.ValidCodeUtil;
import com.lebang.component.RedisClient;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.member.LbbMember;
import com.lebang.model.ValidCodeRecord;
import com.lebang.service.buss.MemberService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.common.UUIDUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static com.lebang.common.validcode.RedisLock.LOCKPHONECODE;

/**
 * @Author: hwq
 * @Date: 2019/10/28 10:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/new/login")
public class LoginApi {
    @Autowired
    private MemberService memberService;
    @Autowired
    private ValidCodeUtil validCodeUtil;
    @Autowired
    private RedisClient redisClient;

    /**
     * 新版登录
     * 用户存在则登录 不存在则注册并登录
     *
     * @param request
     * @param phone
     * @param code
     * @return
     */
    @PostMapping("/register")
    @Sessionid
    public JsonData login(HttpServletRequest request,
                          @RequestParam(value = "phone") String phone,
                          @RequestParam(value = "code") String code,
                          @RequestParam(value = "params", required = false) String params,
                          @RequestParam(value = "isRoot", required = false) Integer isRoot) throws Exception {

        return memberService.validCodeRegister(request,phone,code,params);

    }

    /**
     * 登录验证码
     *
     * @param phone
     * @param request
     * @param response
     * @param session
     * @return
     * @author weizx
     * @date 2018年7月25日 下午2:31:30
     */
    @Sessionid
    @RequestMapping(value = "/valid", method = {RequestMethod.GET, RequestMethod.POST})
    public JsonData loginCode(@RequestParam(value = "phone",required = false) String phone, HttpServletRequest request,
                              HttpServletResponse response, HttpSession session) {
        try {
            RequestHeaders requestHeaders = new RequestHeaders(request);
            if(TextUtil.isEmpty(phone)){
                return Callback.fail("fail","手机号不能为空!");
            }
            if (!TextUtil.isMobile(phone)) {
                return Callback.fail("invalid_phone", "请输入正确的手机号！");
            }
            //加锁防止重复发
            String uuid = UUIDUtil.M();
            boolean a = redisClient.tryGetDistributedLock(LOCKPHONECODE.getName() + phone, uuid, 3000);
            if (a) {
                if(memberService.checkImeiAndPhone(phone,requestHeaders)){
                    return Callback.fail("fail", "一个设备只能登录一个手机号", null);
                }
                ValidCodeStatus validCode = validCodeUtil.getValidCode(phone, new ValidCodeRecord(request));
                if (validCode.isStatus()) {
                    //验证码存入redis中
                    redisClient.setex(RedisLogin.PHONE_NEW_LOGIN_CODE.getName() + phone, 300, validCode.getCode());

                    return Callback.success("success", "发送成功", "");
                } else {
                    return Callback.fail("fail",  validCode.getMsg(), null);
                }
            } else {
                return Callback.fail("fail", "请勿重复操作哦", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Callback.fail("error", e.getMessage());
        }
    }

    /**
     * 一键登录
     *
     * @param request
     * @param token
     * @return
     */
    @Sessionid
    @PostMapping("/fastlogin")
    public JsonData fastlogin(HttpServletRequest request, String token
            , @RequestParam(value = "params", required = false) String parameter
            , @RequestParam(value = "isRoot", required = false) Integer isRoot) {
        return memberService.fastRegister(request,token,parameter);
    }

    /**
     * 退出登录请求接口
     *
     * @param request
     * @return
     */
    @Require
    @Sessionid
    @PostMapping("/out")
    public JsonData out(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        LbbMember member = memberService.selectMemberById(requestHeaders.getUserid());
        memberService.insertLoginRecord(member.getPhone(),requestHeaders);
        return Callback.success("200", "退出登录成功");
    }


}
